#include "psp.h"
#include "pspevent.h"

#define MODULE_NAME "mpdclient"
#define true 1
#define printf pspDebugScreenPrintf

PSP_MODULE_INFO(MODULE_NAME, 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);



PSP_HEAP_SIZE_MAX ();

/* Manejador del callback de salida */
int exit_callback(int arg1, int arg2, void *common)
{
    sceKernelExitGame();
    return 0;
}

/* Crea el callback de salida */
int CallbackThread(SceSize args, void *argp)
{
    int cbid;
    cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
    sceKernelRegisterExitCallback(cbid);
    sceKernelSleepThreadCB();
    return 0;
}

/* Crea y configura el hilo para el callback y retorna su ID */
int SetupCallbacks(void)
{
    int thid = 0;

    thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
    if (thid >= 0)
    {
        sceKernelStartThread(thid, 0, 0);
    }

    return thid;
}


int connect_to_apctl(int config)
{
    int err;
    int state;
    int stateLast = -1;
    char stateinfo[25];
    //char st_text[50];
    char text[100];

    if ((err = sceNetApctlConnect(config)) < 0)
    {
        sprintf(text, "sceNetApctlConnect returns %x.", err);
        loading_screen_paint(text);
        goto error_connecting;
    }

    while (1)
    {
        if (sceWlanGetSwitchState() != 1)
        {
            loading_screen_paint("WLAN switch is turned off.");
            err = -1;
            goto error_connecting;
        }

        if ((err = sceNetApctlGetState(&state)) < 0)
        {
            sprintf(text,"sceNetApctlGetState returns %x.", err);
            loading_screen_paint(text);
            goto error_connecting;
        }

        if (state != stateLast)
        {
            switch (state)
            {
            case PSP_NET_APCTL_STATE_DISCONNECTED:
                strcpy(stateinfo, "Disconnected");
                break;
            case PSP_NET_APCTL_STATE_SCANNING:
                strcpy(stateinfo, "Scanning");
                break;
            case PSP_NET_APCTL_STATE_JOINING:
                strcpy(stateinfo, "Joining");
                break;
            case PSP_NET_APCTL_STATE_GETTING_IP:
                strcpy(stateinfo, "Obtaining IP address");
                break;
            case PSP_NET_APCTL_STATE_GOT_IP:
                strcpy(stateinfo, "IP address obtained");
                break;
            case PSP_NET_APCTL_STATE_EAP_AUTH:
                strcpy(stateinfo, "EAP Authenticating");
                break;
            case PSP_NET_APCTL_STATE_KEY_EXCHANGE:
                strcpy(stateinfo, "Exchanging key information");
                break;
            }

            sprintf(text,"Connection state: %s.", stateinfo);
            loading_screen_paint(text);
            //print st_text to show connection state
            stateLast = state;


        }

        if (state == 4) goto connected;

        sceKernelDelayThread(50 * 1000);
    }

error_connecting:
    //print st_text to show error
connected:
    return (err < 0)?0:1;
}

int init_psp()
{
    int err;

    SetupCallbacks();
    sceUtilityLoadNetModule(1);
    sceUtilityLoadNetModule(3);


    if((err = pspSdkInetInit())) {
		printf("Error, could not initialise the network");
		return 0;
    }

    return 1;
}


void clean_psp()
{
    sceKernelExitGame();
}

int check_connection_to_apctl()
{
    int state;
    sceNetApctlGetState(&state);
    if(state == PSP_NET_APCTL_STATE_DISCONNECTED)
    {
        loading_screen_init();
        connect_to_apctl(1);
        loading_screen_close();
        return 0;
    }

    return 1;
}
